﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Xml;
using BasePlatformCommon;
using BasePlatformCommon.lwf;

namespace BasePlatformNet
{
	public class XmlReadWrite
	{
		// 从 lwf 文件的 xml 区读取数据
		public static SystemTubeDataStore ParseLwfXmlData(XmlDocument xmlDoc)
		{
			SystemTubeDataStore systemTubeDataStore = null;

			try
			{
				foreach (XmlNode node in xmlDoc.ChildNodes)
				{
					if (node.Name.Equals("SystemTubeDataStore"))
					{
						systemTubeDataStore = ParseSystemTubeDataStore(node);
					}
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message + ex.StackTrace);
				return null;
			}

			return systemTubeDataStore;
		}

		// 解析根 SystemTubeDataStore （xml节点为SystemTubeDataStore）
		private static SystemTubeDataStore ParseSystemTubeDataStore(XmlNode dataNode)
		{
			SystemTubeDataStore systemTubeDataStore = new SystemTubeDataStore();

			foreach (XmlNode node in dataNode.ChildNodes)
			{
				// 解析节点 SystemTube
				if (node.Name.Equals("m_SystemTube"))
				{
					systemTubeDataStore.systemTube = ParseSystemTube(node);
				}
				// 解析节点 SubTube（List）
				if (node.Name.Equals("m_SubTube"))
				{
					systemTubeDataStore.subTube = ParseSubTube(node);
				}
			}

			return systemTubeDataStore;
		}

		// 解析 SystemTube （xml节点为SystemTube）
		private static SystemTube ParseSystemTube(XmlNode parentNode)
		{
			SystemTube systemTube = new SystemTube();

			foreach (XmlNode node in parentNode.ChildNodes)
			{
				if (node.Name.Equals("x"))
				{
					systemTube.x = int.Parse(node.InnerText);
				}
				else if (node.Name.Equals("y"))
				{
					systemTube.y = int.Parse(node.InnerText);
				}
			}

			return systemTube;
		}

		// 解析 SubTube （xml节点为SubTube）
		private static SubTube ParseSubTube(XmlNode dataNode)
		{
			SubTube subTube = new SubTube();
			
			foreach (XmlNode node in dataNode.ChildNodes)
			{
				if (node.Name.Equals("s_num"))
				{
					subTube.s_num = int.Parse(node.InnerText);
				}
			}

			for (int i = 0; i < subTube.s_num; i++)
			{
				foreach (XmlNode node in dataNode.ChildNodes)
				{
					if (node.Name.Equals("s" + i))
					{
						foreach (XmlNode subNode in node.ChildNodes)
						{
							subTube.subDataList.Add(ParseSubData(subNode));
						}
						break;
					}
				}
			}

			return subTube;
		}

		// 解析 SubData （xml节点为PortData）
		private static SubData ParseSubData(XmlNode dataNode)
		{
			SubData subData = new SubData();

			foreach (XmlNode node in dataNode.ChildNodes)
			{
				if (node.Name.Equals("p_num"))
				{
					subData.p_num = int.Parse(node.InnerText);
				}
				else 
				{
					for (int i = 0; i < 8; i++)
					{
						if (node.Name.Equals("p" + i))
						{
							subData.portLampNumList[i] = ParsePortLampNum(node);
						}
					}
				}
			}

			return subData;
		}

		// 解析 SubData 的灯具个数
		private static int ParsePortLampNum(XmlNode dataNode)
		{
			foreach (XmlNode node in dataNode.ChildNodes)
			{
				if (node.Name.Equals("TubePoint"))
				{
					foreach (XmlNode subNode in node.ChildNodes)
					{
						if (subNode.Name.Equals("t_num"))
						{
							return int.Parse(subNode.InnerText);
						}
					}
				}
			}
			
			return 0;
		}
	}
}
